﻿using System;
using System.Data;
using System.Windows.Forms;
using AT_UHF_NET;
using System.Collections.Generic;
using ceTerm.OverProduction;

namespace ceTerm
{
    public partial class ReadRFID : XForm
    {
        int shelfId = -1;

        public ReadRFID()
        {
            InitializeComponent();

            pnlSinyalGucu.Visible = false;
            this.WindowState = FormWindowState.Maximized;
            this.KeyPreview = true;
            timer.Interval =  (int)Settings.OkumaSuresiMs;
            Terminal.SetActivatedForm(this);
            dt.Columns.Add("RollId", typeof(int));
            dt.Columns.Add("ShelfId", typeof(int));
            dt.TableName = "Sayim";
        }

        private void ShowInfo(string info)
        {
            lblStatus.Text = info;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F18: // AT570, AT870 Gun Trigger 
                case Keys.F19: // AT570, AT870 Gun Trigger                                                               
                        Terminal.StartRFID();
                        btnCikar.Enabled = btnTemizle.Enabled = btnDetay.Enabled = btnKaydet.Enabled = false;
                    break;
            }            
            base.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F18: // AT570, AT870 Gun Trigger 
                case Keys.F19: // AT570, AT870 Gun Trigger                                                             
                    Terminal.StopRFID();
                    btnCikar.Enabled = btnTemizle.Enabled = btnDetay.Enabled = btnKaydet.Enabled = true;
                    break;                
            }
            base.OnKeyUp(e);
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            try
            {
                int sayimId = Main.ceWS.InsSayim(Main.Password,Settings.TerminalId, dt);
                MessageBox.Show(sayimId + " No'lu Sayım olarak kaydedildi.");
                Terminal.PlaySuccess();
                dt.Clear();
                lbTop.Items.Clear();
            }
            catch (Exception ex)
            {
                ShowException("Veritabanı hatası!", "Hata", ex);
            }
        }

        //Read Events
        public override void GetAccessEPC(string EPC)
        {
            string[] temp = EPC.Split(",".ToCharArray());
            int EPCint = intParse(temp[0].Substring(4, temp[0].Length - 4));
            if (EPCint < 1)
                return;

            //if (BarkodTipi(EPCint) != barkodTipleri.Top)
            //{
            //    ShowInfo("Geçerli bir top no değil !");                
            //    return;
            //}
            //else 
                if (BarkodTipi(EPCint) == barkodTipleri.RafAraba)
            {
                shelfId = EPCint;
                return;
            }

            bool found = false;

            for (int i = 0; i < lbTop.Items.Count; i++) 
            {
                if (lbTop.Items[i].ToString() == EPCint.ToString())
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                lbTop.Items.Add(EPCint);
                DataRow dr = dt.NewRow();
                dr["RollId"] = EPCint;
                dr["ShelfId"] = shelfId;
                dt.Rows.Add(dr);
                lblHeader.Text = "Okunan Toplar (" + lbTop.Items.Count + ")";                
            }
        }

        private void btnDetay_Click(object sender, EventArgs e)
        {
            if (lbTop.SelectedItem != null)
            {
                int rollId = (int)lbTop.SelectedItem;
                Browse("TopDetay.aspx?TopId=" + rollId.ToString(), true, "Top Detayı");
            }
            else
            {
                MessageBox.Show("Lütfen önce listeden bir top seçiniz.");
            }
        }

        DataTable dt = new DataTable();

        private void btnTemizle_Click(object sender, EventArgs e)
        {
            lbTop.Items.Clear();
            dt.Clear();
            lblHeader.Text = "Okunan Toplar";
        }

        private void btnCikar_Click(object sender, EventArgs e)
        {
            if (lbTop.SelectedItem != null)
            {
                int rollId = (int)lbTop.SelectedItem;
                lbTop.Items.Remove(rollId);
                int index = -1;
                for (int i = 0; i < dt.Rows.Count;i++)
                {
                    if ( (int) dt.Rows[i]["RollId"] == rollId)
                    {
                        index = i;
                          break;
                    }
                }
                dt.Rows.RemoveAt(index);
                lblHeader.Text = "Okunan Toplar (" + lbTop.Items.Count + ")";                
            }
            else
            {
                MessageBox.Show("Lütfen önce listeden bir top seçiniz.");
            }
        }
    }
}